const app = getApp();
const db = wx.cloud.database();
Page({
  data: {
    listData: [],
    listDatasj:[],
    listDataxf:[],
    listDatasp:[],
    listDataqt:[],
   receiverData1:[],
    xianshi: 'shouye' ,
    shouyeneirong:'quanbushangpin',
    productInfo: null,
    hasUserInfo: false,
    userInfo: {},
    users:[],
    chatList: [] // 聊天列表数据
  },
//  显示页面
  showshouye() {
    this.setData({
      xianshi: 'shouye'
    });
  },
  showxiaoxi() {
    this.setData({
      xianshi: 'xiaoxi'
    });
  },
  showwode() {
    this.setData({
      xianshi: 'wode'
    });
  },
  showquanbushangpin() {
    this.setData({
      shouyeneirong: 'quanbushangpin'
    });
  },
  showsusheyongpin() {
    this.setData({
      shouyeneirong: 'susheyongpin'
    });
  },
  showkebenshuji() {
    this.setData({
      shouyeneirong: 'kebenshuji'
    });
  },
  showxiefubaoshi() {
    this.setData({
      shouyeneirong: 'xiefubaoshi'
    });
  },
  showshipinyinliao() {
    this.setData({
      shouyeneirong: 'shipinyinliao'
    });
  },
  showqitahaowu() {
    this.setData({
      shouyeneirong: 'qitahaowu'
    });
  },
 
// 商品分类
  getdata(){
   db.collection('shangpinxinxi').limit(10).get().then(res =>{
     this.setData({
       listData:res.data
     })
   })
  },
  getdatass(){
    db.collection('shangpinxinxi').limit(10).where({
      fenlei:'宿舍用品'
    }).get().then(res =>{
      this.setData({
        listDatass:res.data
      })
    })
   },
   getdatakb(){
    db.collection('shangpinxinxi').limit(10).where({
      fenlei:'课本书籍'
    }).get().then(res =>{
      this.setData({
        listDatakb:res.data
      })
    })
   },
   getdataxf(){
    db.collection('shangpinxinxi').limit(10).where({
      fenlei:'鞋服包饰'
    }).get().then(res =>{
      this.setData({
        listDataxf:res.data
      })
    })
   },
   getdatasp(){
    db.collection('shangpinxinxi').limit(10).where({
      fenlei:'食品饮料'
    }).get().then(res =>{
      this.setData({
        listDatasp:res.data
      })
    })
   },
   getdataqt(){
    db.collection('shangpinxinxi').limit(10).where({
      fenlei:'其他好物'
    }).get().then(res =>{
      this.setData({
        listDataqt:res.data
      })
    })
   },

   getdatausers(){
    db.collection('users').limit(1).get().then(res =>{
      this.setData({
        users:res.data
      })
    })
   },
  // 获取接收信息用户的stdId
  getreceiverData(){
    const app = getApp();
    // if(this.data.receiverData1.stdId == app.globalData.receiverData.stdId)return; 
     const receiverData = app.globalData.receiverData
     console.log(receiverData)
     this.setData({
       receiverData1:receiverData
     })
  },

 
  onLoad: function(options) {
    this.getdata()
    this.getdatass()
    this.getdatakb()
    this.getdataxf()
    this.getdatasp()
    this.getdataqt()
    this.getdatausers()
    this.getreceiverData()
  },
  onShow() {
    // 每次页面显示时获取用户信息（从全局变量）
    const userInfo = app.globalData.userInfo;
    if (userInfo) {
      this.setData({ userInfo });
    } else {
      // 未登录，跳转登录页
      wx.redirectTo({ url: '/pages/login/login' });
    }
  },
  handleLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除全局用户信息
          app.globalData.userInfo = null;
          // 跳转登录页
          wx.redirectTo({ url: '/pages/login/login' });
        }
      }
    });
  },
  
  
})